package com.qf.service.impl;

import com.qf.dao.AccountDao;
import com.qf.dao.CardDao;
import com.qf.dao.LogDao;
import com.qf.dao.ProductDao;
import com.qf.dao.impl.AccountDaoImpl;
import com.qf.dao.impl.CardDaoImpl;
import com.qf.dao.impl.LogDaoImpl;
import com.qf.dao.impl.ProductDaoImpl;
import com.qf.model.Card;
import com.qf.model.Log;
import com.qf.model.User;
import com.qf.service.CardService;
import com.qf.utils.DruidUtil;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;

public class CardServiceImpl implements CardService {
    CardDao cardDao = new CardDaoImpl();
    ProductDao productDao=new ProductDaoImpl();
    AccountDao accountDao = new AccountDaoImpl();
    LogDao logDao = new LogDaoImpl();

    //添加申请的信用卡信息 非事务
    @Override
    public int addCreditCard(int u_id, String c_account, String c_phone) {
        return cardDao.addCreditCard(u_id,c_account,c_phone);
    }


    //根据用户id查询用户是否具有可使用的储蓄卡
    @Override
    public List<Card> findDepositCardByU_id(int u_id) {
        return cardDao.findDepositCardByU_id(u_id);
    }

    //信用卡还款 事务
    @Override
    public int creditCardRepayment(double amount, int p, User user, Card rep_card,Card sav_card) {
        Connection connection= DruidUtil.getConnection();
        //开启事务
        DruidUtil.begin(connection);
        //创建日志对象
        Log log1 = null;
        Log log2 = null;
        int rows1 = 0;
        int rows2 = 0;
        //转出账户还款，
        if(p == 1){
            //app扣款
            //connection,user.getU_id() 用户id,amount 还款金额
            rows1 = productDao.lessenUserMoney(connection,user.getU_id(),amount);
            //日志对象实例化
            log1 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    user.getU_name(),
                    amount,
                    1,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    4,
                    "用户" + user.getU_name()+"还款"+amount+"元"

            );
            //信用卡收款
            //connection,rep_card 待还款的信用卡,amount 还款金额
            rows2 = cardDao.repaymentAmount(connection,rep_card,amount);
            //日志对象实例化
            log2 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    rep_card.getC_id(),
                    amount,
                    2,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    4,
                    "用户信用卡:收款"+amount+"元"
            );

        }else {
            //银行卡扣款
            //connection,sav_card.getC_id() 用于还款的储蓄卡卡号,amount 还款金额
            rows1 =productDao.lessenCardMoney(connection,sav_card.getC_id(),amount);
            //日志对象实例化
            log1 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    sav_card.getC_id(),
                    amount,
                    1,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    4,
                    "用户储蓄卡:还款"+amount+"元"
            );
            //信用卡收款
            //connection,rep_card 待还款的信用卡,amount 还款金额
            rows2 = cardDao.repaymentAmount(connection,rep_card,amount);
            //日志对象实例化
            log2 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    rep_card.getC_id(),
                    amount,
                    2,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    4,
                    "用户信用卡:收款"+amount+"元"
            );
        }
        //日志对象实例化
        int add1 = logDao.addLog(connection, log1);
        int add2 = logDao.addLog(connection, log2);
        //3.添加日志
        /*LogDao logDao=new LogDaoImpl();
        int k=logDao.addLog(connection,log);*/

        if(rows1 == 1 && rows2 == 1 && add1 == 1 && add2 == 1){
            DruidUtil.commit(connection);
            return 1;
        }else {
            DruidUtil.rollback(connection);
            return 0;
        }
    }

    //根据信用卡号，查询信用卡信息
    @Override
    public Card findCreditCardByC_id(String c_id) {
        return cardDao.findCreditCardByC_id(c_id);
    }
    //通过用户id查询是否有信用卡，审批和未审批的都需要
    @Override
    public List<Card> findCreditCardAll(int u_id) {
        return cardDao.findCreditCardAll(u_id);
    }
    //根据用户id查询用户是否具有未审批通过信用卡或已拒绝的信用卡
    @Override
    public List<Card> findCreditCardUnusable(int u_id) {
        return cardDao.findCreditCardUnusable(u_id);
    }

    //查询用户是否有主卡
    @Override
    public List<Card> findHostCard(int u_id) {
        return cardDao.findHostCard(u_id);
    }

    //信用卡转出  事务
    @Override
    public int creditCardOut(Card masterCard,Card creditCard,User user,Double amount,int p,double money) {
        Connection connection = DruidUtil.getConnection();
        //开启事务
        DruidUtil.begin(connection);
        //创建日志对象
        Log log1 = new Log();
        Log log2 = new Log();
        int rows1 = 0;
        int rows2 = 0;
        //p表示接收转出金额的方式
        if (p == 1) {
            //用户账户接收
            //事务,用户id,接收金额
            //Connection conn, int uid 用户id, double money 转入金额
            rows1 = productDao.addUserMoney(connection,user.getU_id(),amount);
            log1 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    user.getU_name(),
                    amount,
                    2,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    2,
                    "用户"+user.getU_name()+":收款"+amount+"元"
            );
            //信用卡卡号 转出
            //Connection conn, String cid 转出卡号, double money 转出金额
            rows2 = productDao.lessenCardMoney(connection,creditCard.getC_id(),money);
            //日志对象实例化
            log2 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    creditCard.getC_id(),
                    money,
                    1,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    2,
                    "信用卡:转出"+money+"元"
            );
        } else {
            //主卡接收
            //Connection conn, String cid 转入卡卡号, double amount 转入金额
            rows1 = productDao.addCardMoney(connection,masterCard.getC_id(),amount);
            log1 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    masterCard.getC_id(),
                    amount,
                    2,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    3,
                    "用户主卡:接收"+amount+"元"
            );
            //Connection conn, String cid 转出卡号, double money 转出金额
            rows2 = productDao.lessenCardMoney(connection,creditCard.getC_id(),money);
            //日志对象实例化
            log2 = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    creditCard.getC_id(),
                    money,
                    1,
//                new Date(new java.util.Date().getTime()),
                    new Timestamp(new java.util.Date().getTime()),
                    2,
                    "信用卡：转出"+money+"元"
            );
        }

        int add1 = logDao.addLog(connection, log1);
        int add2 = logDao.addLog(connection, log2);
        //3.添加日志
        /*LogDao logDao=new LogDaoImpl();
        int k=logDao.addLog(connection,log);*/

        if (rows1 == 1 && rows2 == 1 && add1 == 1 && add2 == 1) {
            DruidUtil.commit(connection);
            return 1;
        } else {
            DruidUtil.rollback(connection);
            return 0;
        }
    }
    //审核或者拒绝未审核的银行卡，或者信用卡
    @Override
    public int updateCard(int id, int p) {
        return cardDao.updateCard(id,p);
    }

    //根据银行卡卡号查询银行卡信息
    public Card findCard(String c_id){
        return cardDao.findCard(c_id);
    }
}
